local hx__sheru = fk.CreateSkill {
  name = "hx__sheru",
}

hx__sheru:addEffect("active", {
  anim_type = "drawcard",
  card_num = 0,
  target_num = 1,
  prompt = "#hx__sheru",
  can_use = function(self, player)
    if player:isKongcheng() then return false end
    return player:usedSkillTimes(hx__sheru.name, Player.HistoryPhase) < 2
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected, selected_cards)
    if #selected > 0 or to_select == player then return false end
    return player:canPindian(to_select)
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    local pindian = player:pindian({target}, hx__sheru.name)
    if pindian.results[target].winner == player then
      player.room:drawCards(player, 1, hx__sheru.name)
      local ids = {}
      for _, card in ipairs({pindian.fromCard, pindian.results[target].toCard}) do
        if room:getCardArea(card) == Card.DiscardPile then
          table.insertIfNeed(ids, card:getEffectiveId())
        end
      end
      if #ids > 0 and not player.dead then
        room:moveCards({
          ids = ids,
          to = player,
          toArea = Player.Hand,
          moveReason = fk.ReasonPrey,
          proposer = player,
          skillName = hx__sheru.name,
        })
      end
    else
      room:invalidateSkill(player, hx__sheru.name, "-turn")
    end
  end,
})

return hx__sheru